//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications.Implementations
{
    using System.Security.Cryptography.X509Certificates;
    using Model;

    public class CertificateVerification : VerificationBase<CertificateAccessVerificationDescription>
    {
        protected override bool OnBeforeVerification(VerificationResult result)
        {
            return this.Description.GetStoreLocation.SuccessfullyYieldsValue("Store location", result) &&
                   this.Description.GetStoreName.SuccessfullyYieldsValue("Store name", result) &&
                   this.Description.GetThumbprint.SuccessfullyYieldsValue("Certificate thumbprint", result) &&
                   this.Description.GetThumbprint.DoesNotYieldNullValueOrEmpty("Certificate thumbprint", result);
        }

        protected override void AddVerificationDetail(VerificationResult result)
        {
            result.AddMessage(MessageType.Information, "Certificate Thumbprint: {0}", this.Description.GetThumbprint());
            result.AddMessage(MessageType.Information, "Store Name: {0}", this.Description.GetStoreName());
            result.AddMessage(MessageType.Information, "Store Location: {0}", this.Description.GetStoreLocation());
        }

        protected override void ExecuteVerification(VerificationResult result)
        {
            var store = new X509Store(this.Description.GetStoreName(), this.Description.GetStoreLocation());
            store.Open(OpenFlags.ReadOnly);
            try
            {
                if (this.CertificateExists(result, store) == false)
                {
                    result.AddMessage(MessageType.Error, "Certificate not found");
                }
            }
            finally
            {
                store.Close();
            }
        }

        private bool CertificateExists(VerificationResult result, X509Store store)
        {
            var targetThumbprint = this.Description.GetThumbprint();

            var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, targetThumbprint, false);
            foreach (var certificate in certificates)
            {
                var subject = certificate.Subject;
                certificate.Reset();
                result.AddMessage(MessageType.Information, "Certificate found. Subject: {0}", subject);
                break;
            }

            return certificates.Count > 0;
        }
    }
}
